<?php
class News_m extends CI_Model{
	function __construct(){
		parent ::__construct();
		$this->load->database();
	}
	
	//--count news--//
	function count_news(){
		$query = $this->db->select("news.*, company.name as company_name")
						  ->from("news")
						  ->join("company", "company.id = news.company")
						  ->where("news.type", "actualite")
						  ->get();
		if($query){
			return $query->num_rows();
		}else{
			return FALSE;
		}
						  
	}
	
	//--list news--//
	function list_news($type, $perpage, $offset){
		$query = $this->db->select("news.*, company.name as company_name, users.user_name")
						  ->from("news")
						  ->join("company", "company.id = news.company")
						  ->join("users", "users.id = news.user_post")
						  ->where("news.type", $type)
						  ->limit($perpage, $offset)
						  ->get();
		if($query){
			return $query->result_array();
		}else{
			return FALSE;
		}
						  
	}
	
	//--insert news into database--//
	function ajouter_actualite($additional_data){
		$fields = array("title", "description", "content", "user_post", "post_time", "active", "company", "image", "type");
		$data = array();
		foreach($fields as $field){
			if(is_array($additional_data) && (isset($additional_data[$field]))){
				$data[$field] = $additional_data[$field];
			}elseif ($this->input->post()){
				$data[$field] = $this->input->post($field);
			}	
		}
		
		$query = $this->db->insert("news", $data);
		return $this->db->affected_rows() ? true : false;
		
	}
		
	//--update news into database--//
	function modifier_actualite($id, $additional_data){
		$fields = array("title", "description", "content", "post_time", "active", "company", "image", "type");
		$data = array();
		foreach($fields as $field){
			if(is_array($additional_data) && (isset($additional_data[$field]))){
				$data[$field] = $additional_data[$field];
			}elseif ($this->input->post()){
				$data[$field] = $this->input->post($field);
			}	
		}
		
		$query = $this->db->update("news", $data, array("id"=>$id));
		return $this->db->affected_rows() ? true : false;
		
	}
	
	//--get company data for dropdown list--//
	function _get_dd_company($to_edit = FALSE){
		 $query  = 	$this->db->select("id, name")
							 ->from("company")
							 ->order_by("id", "asc")
							 ->get();
		$companies = array();
		if($to_edit == TRUE){
			unset($companies[-1]);	
		}else{
			$companies[-1] = "Sélectionnez";	
		}
		
		if($query->num_rows() > 0){
			foreach($query->result() as $company){
				$companies[$company->id] = $company->name;
			}
		}else{
			return FALSE;
		}
		return $companies;
		
	}
	
	//--get news details--//
	function _get_news_details($id){
		$query = $this->db->select("news.*, company.name as company_name, users.user_name")
						  ->from("news")
						  ->join("company", "company.id = news.company")
						  ->join("users", "users.id = news.user_post")
						  ->where("news.id", $id)
						  ->limit(1)
						  ->get();
		if($query->num_rows() >0 ){
			return $query;
		}else{
			return FALSE;
		}
	}
	
	//--delete news--//
	function delete_news($id){
		$query = $this->db->delete("news", array("id"=>$id));
		return $this->db->affected_rows() ? true : false;
	}
	
	//--delete multi news--//
	function delete_multi_news($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('news');
        return $query;
    }
    

	//--count communique--//
	function count_communique(){
		$query = $this->db->select("news.*, company.name as company_name")
						  ->from("news")
						  ->join("company", "company.id = news.company")
						  ->where("news.type", "communique")
						  ->get();
		if($query){
			return $query->num_rows();
		}else{
			return FALSE;
		}
						  
	}
	
	
	
	
}